var e, t = (e = require("@vant/weapp/toast/toast")) && e.__esModule ? e : {
    default: e
};
var a = require("../../utils/request"), r = require("../../utils/storage"), s = require("../../utils/config"), i = require("../../utils/encrypt");
Component({
    properties: {},
    data: {
        themeColor: getApp().globalData.themeColor,
        showLoginButton: !0,
        needlogin:!0,
        showModal: !1,
        userBase: {
            avatar200: ""
        },
        nickName: "",
        avatarUrl: "",
        show:false,
        phoneshow:false,

    },
    lifetimes: {
        attached: function() {},
        detached: function() {},
        ready: function() {
            console.log("geren32222");
            this.setHeader();
            this.getUinfo();  
        }
    },
    methods: {

        getUinfo: function() {
            var e = this;
            wx.showLoading({
                title: "正在加载中..."
            });
            var t = r.getStorageSync(r.storageKey.openId);
           
            a.post1("/wechat/Member/getUserInfo", {
                openid: t
            }).then(function(t) {
                if (wx.hideLoading(), "1" == t.code) {
                
                    wx.setStorageSync('userBase', t.data);
                    wx.setStorageSync('uid',t.data.id);
                    wx.setStorageSync('phoneNumber', t.data.mobile);


                      if( t.data.alias_nickname&& t.data.headimgurl){
                        e.setData({avatarUrl:getApp().globalData.baseImgUrl+ t.data.headimgurl,needlogin:0,nickName: t.data.alias_nickname}); 
                       
                      }   
                } else {

                    wx.showModal({
                        title: "提示",
                        content: "暂无数据",
                        showCancel: !1
                    }); 
                };
            });
        },

        
        avatarClick: function() {
            this.data.showLoginButton || wx.navigateTo({
                url: "/pages/myinfo/myinfo"
            });
        },

        onChooseAvatar(e){
            const { avatarUrl } = e.detail;
            if(avatarUrl){
              wx.showLoading();
              this.headUploadImgs(avatarUrl)
            }
          },

          headUploadImgs(filePath) {
              let that=this;
            wx.uploadFile({
              url:getApp().globalData.baseUrl + "common/index/uploadImg",  
              filePath: filePath,
              name: 'file',
              success: (res) => {
                if (res.data) {
                  let json = JSON.parse(res.data)
                  if (json.code == 1) {
                    that.setData({avatarUrl:filePath});  
                    this.headImgWrite(json.data)
                  } else {
                    wx.showToast({
                      title: '上传失败',
                      icon: 'none'
                    })
                  }
                } else {
                  
                }
              }
            })


          },
          headImgWrite(url) {
 
            var that=this;
            // return post("/wechat/home/setMemberInfo", data);
 
            a.post1("/wechat/home/setMemberInfo", {
                headimgurl:url,
                flag: 2,
                openid:r.getStorageSync(r.storageKey.openId)
            }).then(function(e) {
 
                if ("1" == e.code) {
                    // this.avatarUrl=url;
                    wx.hideLoading()
                    // ,avatarUrl:url
                    that.setData({show:true});
                    // that.setData({show:true});
                };
            })
          
          }, 
          onClose() {
            this.setData({show:false});  
          },


          bindphone() {
            this.setData({phoneshow:true});
            
          },

          onClosephone() {
            this.setData({phoneshow:false});  
          },
          getphone(e){
            this.phone=e.detail.value;
          },
           
          onSubmitphone(e){
            if(!this.phone){
              return wx.showToast({
                title: '请填写手机号',
                icon: 'none'
              })
            }
            wx.showLoading();
            wapi.default.setMemberInfo({
                mobile:this.phone
            }).then(res => {
                if(res.code ==1){
                    wx.hideLoading();
                    this.setData({showphone:false});
                    wx.showToast({
                      title: '成功！',
                      icon: 'success'
                    })
                    this.setData({phoneshow:false});  
                    this.getUserInfo();
                }else{
                    return wx.showToast({
                        title: res.msg,
                        icon: 'none'
                      })
                }
        
           
            })
          },

          getNickName(e){
            this.nickname=e.detail.value;
          },
          onSubmit(e){
            let that=this;  
            if(!this.nickname){
              return wx.showToast({
                title: '请填写昵称',
                icon: 'none'
              })
            }
            wx.showLoading();
   // return post("/wechat/home/setMemberInfo", data);
            a.post1("/wechat/home/setMemberInfo", {
                alias_nickname:this.nickname,
                flag: 2,
                openid:r.getStorageSync(r.storageKey.openId)
            }).then(function(e) {
 
                if ("1" == e.code) {

                    wx.hideLoading();
                    that.setData({show:false});
                    wx.showToast({
                      title: '登录成功！',
                      icon: 'success'
                    })
                    that.getUinfo();
 
                    // this.avatarUrl=url;
                    // wx.hideLoading()
                    // that.setData({show:true,avatarUrl:url});
                    // this.setData({show:true});
                    // this.setData({show:false});
                };
            }) 
 
          },


 
        removeUserBase: function() {
            this.setData({
                userBase: {
                    avatar200: s.resourceRoot + "avatar.png"
                },
                nickName: "",
                showLoginButton: !0
            });
        },
        setHeader: function() {
            if (r.isLogin()) {
                var e = r.getStorageSync(r.storageKey.userBase), t = r.getStorageSync(r.storageKey.phoneNumber), a = i.encryptMobile(t);
                this.setData({
                    userBase: e,
                    nickName: a,
                    showLoginButton: !1
                });
            } else this.setData({
                userBase: {
                    avatar200: s.resourceRoot + "avatar.png"
                },
                nickName: "",
                showLoginButton: !0
            });
        },
        getUserProfile: function() {
            r.isLogin() || wx.navigateTo({
                url: "/pages/authorize/authorize?path=my"
            });
        },
        closeLogin: function() {
            this.setData({
                showModal: !1
            });
        }
    }
});